package net.ojbk.dao;

import java.sql.*;


import net.ojbk.database.DatabaseConnection;
import net.ojbk.po.WebStatus; // po层  数据实体类

/**
 * ActivationData Table
 * 
 * 增加 一条记录
 * 删除 一条记录
 * 查询 一条记录
 * 修改 一条记录
 * 
 * @author twobox
 *
 */
public class WebStatusDao {
	static final String tableName = "webStatus";
	DatabaseConnection dbconn; // 数据库连接控制对象
	Statement stmt; // sql语句 操作对象
	
	public WebStatusDao() {
		dbconn = new DatabaseConnection();
		stmt = dbconn.getStatement();
	}
	public void finalize() {
		dbconn.close();
	}
	
	// activationData(email, code, password)
	
	public boolean addOneRecord(WebStatus ws) {
		boolean flag = false;
		try {
			String sql = "INSERT INTO "
					+ tableName
					+ " VALUE('"
					+ ws.getD()
					+ "', '"
					+ ws.getUserAmount()
					+ "', '"
					+ ws.getVisitorAmount()
					+ "');";
			stmt.executeUpdate(sql);
			flag = true;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean deleteOneRecord(WebStatus ws) {
		boolean flag = false;
		try {
			// DELETE FROM activationData WHERE email='1@ojbk.net';
			String sql = "DELETE FROM "
					+ tableName
					+ " WHERE d='"
					+ ws.getD()
					+ "';";
			stmt.executeUpdate(sql);
			flag = true;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean updateOneRecord(WebStatus ws) {
		boolean flag = false;
		try {
			// UPDATE activationData SET code='124412412jk4kj124k', password='654321' WHERE email='1@ojbk.net';
			String sql = "UPDATE "
					+ tableName
					+ " SET d='"
					+ ws.getD()
					+ "', userAmount='"
					+ ws.getUserAmount()
					+ "', visitorAmount='"
					+ ws.getVisitorAmount()
					+ "' WHERE d='"
					+ ws.getD()
					+ "';";
			stmt.executeUpdate(sql);
			return true;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean queryOneRecord(WebStatus ws) {
		boolean flag = false;
		ResultSet re;
		try {
			// SELECT * FROM activationData WHERE email='1@ojbk.net';
				String sql;
				sql = "SELECT * FROM "
						+ tableName
						+ " WHERE d='"
						+ ws.getD()
						+ "';";
			re = stmt.executeQuery(sql);
			if (re.next()) {
				ws.setD(re.getString("d"));
				ws.setUserAmount(re.getInt("userAmount"));
				ws.setVisitorAmount(re.getInt("vistorAmount"));
				flag = true;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public static void main(String[] args) {
		WebStatus ws=new WebStatus();
		WebStatusDao wsd=new WebStatusDao();
		ws.setD("2018-03-19");
		//ws.setUserAmount(12345);
		//ws.setVisitorAmount(54321);
		//wsd.addOneRecord(ws);
		//wsd.deleteOneRecord(ws);
		ws.setUserAmount(233);
		ws.setVisitorAmount(233233);
		wsd.updateOneRecord(ws);
	}
	
}